<template>
	<view class="outsize">
		<u-navbar :title=title @rightClick="rightClick" :autoBack="true" placeholder='true'>
		</u-navbar>
		<view class="term" v-for="(item,index) in substance" :key="index" v-if="title == '我的租约'||title == '我的关注'">
			<view class="follow">
				<view style="display: flex;">
					<image :src='item.houseRoom.faceUrl' mode="widthFix"
						style="width: 20vw;display: flex;align-items: center;">
					</image>
					<view
						style="margin-left: 10px; font-family: fangsong;display: flex; line-height: 25px; flex-wrap: wrap;align-items: center;">
						<view style="width: 100%;color: #FA0000;display: flex;">
							¥{{item.houseRoom.price}}/
							<view style="font-size: 10px;">
								{{item.houseRoom.payType}}
							</view>
						</view>
						<view style="width: 100%;">
							{{item.houseRoom.villageName}}{{item.houseRoom.address}}
						</view>
						<view style="width: 100%;">
							{{item.houseRoom.type == '0' ? item.houseRoom.houseArea : item.houseRoom.roomArea}}㎡
						</view>
					</view>
				</view>
			</view>
			<view class="press" style="width: 130px;" v-if="title == '我的租约'">
				<button @click="detailss(item.id)">删除</button>
				<button @click="houseId(item.id)">查看</button>
			</view>
			<view class="press" v-if="title == '我的关注'">
				<button @click="showings(item.houseId)">预约看房</button>
				<button @click="houseId(item.houseId)">查看</button>
				<button @click="phone(item.houseRoom.ownerPhone)">联系房东</button>
			</view>
		</view>
		<view class="term" v-for="(item,index) in substance" :key="index" v-else>
			<view class="follow">
				<view style="display: flex;">
					<image :src='item.faceUrl' mode="widthFix" style="width: 20vw;display: flex;align-items: center;">
					</image>
					<view
						style="margin-left: 10px; font-family: fangsong;display: flex; line-height: 25px; flex-wrap: wrap;align-items: center;">
						<view style="width: 100%;color: #FA0000;display: flex;">
							¥{{item.price}}/
							<view style="font-size: 10px;">
								{{item.payType}}
							</view>
						</view>
						<view style="width: 100%;">
							{{item.villageName}}{{item.address}}
						</view>
						<view style="width: 100%;">
							{{item.type == '0' ? item.houseArea : item.roomArea}}㎡
						</view>
					</view>
				</view>
			</view>
			<view class="press" v-if="title == '历史发布'">
				<button @click="detail(item.id)">撤回</button>
				<button @click="houseId(item.id)">查看</button>
				<button @click="modify(item.id)">修改</button>
			</view>
		</view>
		<u-empty v-if="substance.length==0" mode="history" icon="/static/index/homeowner/history.png">
		</u-empty>
		<!-- </view> -->
	</view>
</template>

<script setup>
	import {
		onLoad,
		onShow
	} from "@dcloudio/uni-app"
	import {
		reactive,
		ref
	} from "vue";
	import {
		get,
		post
	} from '@/serve/method.js'
	const title = ref()
	const userId = ref()
	const substance = ref([])
	const faceRecognition = reactive({
		houseId: '',
		dISCRIMINATION: '',
	})
	// 打电话
	const phone = (item) => {
		//拨打固定电话
		uni.makePhoneCall({
			phoneNumber: item,
		});
	}
	//我的关注的删除
	const detailss = (item) => {
		let url = `/api/system/rental/remove?ids=${item}`
		post(url).then(res => {
			substance.value.length = 0
			ni()
		});
	}

	// 我的关注去看房
	const showings = (id) => {
		uni.getStorage({
			key: 'lifeData',
			success: function(resone) {
				let url = "/api/user/selectUserById"
				get(url, {
					userId: resone.data.vuex_user.userId
				}).then((res) => {
					console.log(res.data.authStatus, "验证")
					if (res.data.authStatus !== "2" && res.data.authStatus !== "1") {
						uni.showModal({
							title: '提示',
							content: '本平台为诚信共享，便民互助平台，为保障您的权益及更好的为您服务，请进行认证。',
							success: function(res) {
								uni.navigateTo({
									url: '/pages/mine/authentication'
								});
							}
						});
					} else {
						faceRecognition.dISCRIMINATION = 'true'
						faceRecognition.houseId = id
						console.log(resone.data.vuex_user.userId, 9999999)
						let urls = `/api/user/face?userId=${resone.data.vuex_user.userId}`
						get(urls).then((res) => {
							console.log(res)
							wx.navigateToMiniProgram({
								appId: 'wx1cf2708c2de46337', // 公证签小程序APPID
								path: `/pages/face/index?bizToken=${res.data}`, // 刷脸页面地址
							})
						})
					}
				})
			},
			fail: function(res) {
				if (res.errMsg == 'getStorage:fail data not found') {
					uni.reLaunch({
						url: '/pages/index/index'
					});
				}
			}
		})
	}
	onShow(() => {
		if (faceRecognition.dISCRIMINATION == 'true') {
			let recognition = wx.getEnterOptionsSync()
			// options数据在上方
			console.log('---options1', recognition)
			console.log('---options2', recognition.referrerInfo.extraData.faceResult.ErrorMsg)
			console.log('---options3', recognition.referrerInfo.extraData.faceResult.ErrorMsg.substring(0, 2))
			if (recognition.referrerInfo.extraData.faceResult.ErrorMsg.substring(0, 2) == "成功") {
				faceRecognition.dISCRIMINATION = 'false'
				uni.navigateTo({
					url: `/pages/houseViewing/appointment?id=${faceRecognition.houseId}`
				});
			}
		}
	})
	// 历史发布的撤回
	const detail = (houseId) => {
		let url = "/api/house/cacelPushHouse";
		post(url, {
			state: 2,
			id: houseId,
			publishId: userId.value
		}).then(res => {
			substance.value.length = 0
			ni()
		});
	}
	const houseId = (houseId) => {
		// console.log(houseId.houseId, 9999)
		console.log(houseId, 88888)
		uni.navigateTo({
			url: `/pages/detail/detail?houseId=${houseId}`
		});
	}
	const modify = (houseId) => {
		uni.navigateTo({
			url: `/pages/detail/preHouse?xiugai=${houseId}`
		});
	}
	// 
	onLoad((option) => {
		console.log(option.id)
		title.value = option.id
		uni.getStorage({
			key: 'lifeData',
			success: function(res) {
				userId.value = res.data.vuex_user.userId
				ni()
			}
		})

	})
	const ni = () => {
		if (title.value == '历史发布') {
			let urls = "/api/system/room/listbyPublishId"
			post(urls, {
				"publishId": userId.value,
			}).then((res) => {
				substance.value = res.rows
				console.log(substance.value, 333)
			})
		} else if (title.value == '我的租约') {
			console.log('我的租约')
			let urls = `/api/system/rental/listByUserId/${userId.value}`
			post(urls).then((res) => {
				substance.value = res.rows
				console.log(substance.value, 333)
			})
		} else if (title.value == '我的关注') {
			console.log('我的关注')
			let url = "/api/system/house/list"
			let that = this
			post(url, {
				userId: userId.value,
				status: 1
			}).then(data => {
				console.log(data, 9999)
				substance.value = data.rows
			});
		}
	}
</script>

<style scoped lang="less">
	.outsize {
		background-color: #F2F2F2;
		width: calc(100vw - 20px);
		height: calc(100vh - 20px);
		overflow-y: scroll;
		padding: 10px 10px;

		.term {
			margin-bottom: 20px;
			background-color: #ffffff;

			.follow {
				display: flex;
				justify-content: space-between;
				padding: 10px 10px;
				align-items: center;

			}

			.press {
				display: flex;
				flex-direction: row-reverse;
				width: 60vw;
				margin-left: auto;

				button {
					border-radius: 5px;
					height: 25px;
					font-size: 12px;
					line-height: 25px;
					margin-top: 3px;
					margin-bottom: 10px;
				}
			}
		}
	}
</style>